<template>
  <div 
    :class="[
      'bx-collapse-panel',
      {
        'bx-collapse-panel--active': isActive
      }
    ]"
  >
    <div 
      class="bx-collapse-panel__header bx-glass"
      @click="togglePanel"
    >
      <div class="bx-collapse-panel__title">
        <slot name="header">{{ header }}</slot>
      </div>
      <BxIcon 
        name="chevron-right"
        :class="[
          'bx-collapse-panel__arrow',
          {
            'bx-collapse-panel__arrow--active': isActive
          }
        ]"
      />
    </div>
    
    <div 
      v-show="isActive"
      class="bx-collapse-panel__content-wrapper"
    >
      <div class="bx-collapse-panel__content">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, inject } from 'vue'
import BxIcon from '../basic/BxIcon.vue'

export default {
  name: 'BxCollapsePanel',
  components: {
    BxIcon
  },
  props: {
    name: {
      type: [String, Number],
      required: true
    },
    header: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const collapseContext = inject('collapseContext')

    const isActive = computed(() => {
      return collapseContext.activeNames.value.includes(props.name)
    })

    const togglePanel = () => {
      collapseContext.handleItemClick(props.name)
    }

    return {
      isActive,
      togglePanel
    }
  }
}
</script>

<style scoped>
.bx-collapse-panel {
  border-bottom: 1px solid var(--bx-glass-border);
}

.bx-collapse-panel:last-child {
  border-bottom: none;
}

.bx-collapse-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color var(--bx-transition-medium);
}

.bx-collapse-panel__header:hover {
  background: var(--bx-glass-bg-hover);
}

.bx-collapse-panel__title {
  font-size: 15px;
  font-weight: 500;
  color: var(--bx-text-primary);
}

.bx-collapse-panel__arrow {
  font-size: 12px;
  color: var(--bx-text-secondary);
  transition: transform var(--bx-transition-medium);
}

.bx-collapse-panel__arrow--active {
  transform: rotate(90deg);
}

.bx-collapse-panel__content-wrapper {
  overflow: hidden;
  transition: height 0.3s ease-in-out;
}

.bx-collapse-panel__content {
  padding: 16px;
  color: var(--bx-text-secondary);
  font-size: 14px;
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-collapse-panel__header {
    padding: 10px 14px;
  }
  
  .bx-collapse-panel__title {
    font-size: 14px;
  }
  
  .bx-collapse-panel__content {
    padding: 12px 14px;
    font-size: 13px;
  }
}
</style>

